/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.DisUserRight;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.DisUserRightRepository;
import me.zhengjie.modules.system.service.DisUserRightService;
import me.zhengjie.modules.system.service.dto.DisUserRightDto;
import me.zhengjie.modules.system.service.dto.DisUserRightQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.DisUserRightMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author 王宇
* @date 2024-01-06
**/
@Service
@RequiredArgsConstructor
public class DisUserRightServiceImpl implements DisUserRightService {

    private final DisUserRightRepository disUserRightRepository;
    private final DisUserRightMapper disUserRightMapper;

    @Override
    public Map<String,Object> queryAll(DisUserRightQueryCriteria criteria, Pageable pageable){
        Page<DisUserRight> page = disUserRightRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(disUserRightMapper::toDto));
    }

    @Override
    public List<DisUserRightDto> queryAll(DisUserRightQueryCriteria criteria){
        return disUserRightMapper.toDto(disUserRightRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DisUserRightDto findById(Long rightId) {
        DisUserRight disUserRight = disUserRightRepository.findById(rightId).orElseGet(DisUserRight::new);
        ValidationUtil.isNull(disUserRight.getRightId(),"DisUserRight","rightId",rightId);
        return disUserRightMapper.toDto(disUserRight);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DisUserRightDto create(DisUserRight resources) {
        return disUserRightMapper.toDto(disUserRightRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DisUserRight resources) {
        DisUserRight disUserRight = disUserRightRepository.findById(resources.getRightId()).orElseGet(DisUserRight::new);
        ValidationUtil.isNull( disUserRight.getRightId(),"DisUserRight","id",resources.getRightId());
        disUserRight.copy(resources);
        disUserRightRepository.save(disUserRight);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long rightId : ids) {
            disUserRightRepository.deleteById(rightId);
        }
    }

    @Override
    public void download(List<DisUserRightDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DisUserRightDto disUserRight : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户openId", disUserRight.getOpenId());
            map.put("权益类型，0：线下售卖，1：线下赠送", disUserRight.getRightType());
            map.put("权益状态，0：无效，1：有效，-1：删除", disUserRight.getRightStatus());
            map.put("权益分类，0：课程", disUserRight.getRightCategory());
            map.put("权益内容", disUserRight.getRightContent());
            map.put("源openId", disUserRight.getOriginalOpenId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}